package com.itheima.reggie.service.impl;

import cn.hutool.core.collection.CollectionUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.google.gson.internal.$Gson$Preconditions;
import com.itheima.reggie.common.CustomException;
import com.itheima.reggie.domain.Category;
import com.itheima.reggie.domain.Dish;
import com.itheima.reggie.domain.DishFlavor;
import com.itheima.reggie.mapper.CategoryMapper;
import com.itheima.reggie.mapper.DishFlavorMapper;
import com.itheima.reggie.mapper.DishMapper;
import com.itheima.reggie.service.DishService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
@Transactional
@Slf4j
public class DishServiceImpl implements DishService {

    @Autowired
    private CategoryMapper categoryMapper;

    @Autowired
    private DishMapper dishMapper;

    @Autowired
    private DishFlavorMapper dishFlavorMapper;

    @Override
    public Page<Dish> findByPage(Integer pageNum, Integer pageSize, String name) {
        //1. 在菜品表中根据name分页查询
        //1-1 准备分页对象
        Page<Dish> page = new Page<>(pageNum, pageSize);

        //1-2 准备封装查询条件
        //StringUtils.isNotEmpty(name) 判断name是否为非空字符串
        LambdaQueryWrapper<Dish> wrapper = new LambdaQueryWrapper<>();
        wrapper.like(StringUtils.isNotEmpty(name), Dish::getName, name);

        page = dishMapper.selectPage(page, wrapper);
        //2 遍历菜品列表
        List<Dish> dishList = page.getRecords();
        if (CollectionUtil.isNotEmpty(dishList)) {
            for (Dish dish : dishList) {
                //3. 根据菜品的分类id从分类表中查询分类信息(名称)
                Category category = categoryMapper.selectById(dish.getCategoryId());
                dish.setCategoryName(category.getName());
            }
        }
        return page;
    }

    @Override
    public void save(Dish dish) {
        //1. 向dish表保存基本信息,(mybatisplus自动返回主键)
        dishMapper.insert(dish);
        log.info("保存之后的菜品主键:{}", dish.getId());

        //2. 遍历前端传入的口味列表信息
        List<DishFlavor> flavorList = dish.getFlavors();
        if (CollectionUtil.isNotEmpty(flavorList)) {
            for (DishFlavor dishFlavor : flavorList) {
                //3- 为口味设置所属菜品id
                dishFlavor.setDishId(dish.getId());
                //4. 保存口味
                dishFlavorMapper.insert(dishFlavor);
            }
        }
    }

    @Override
    public Dish findById(Long id) {
        //1. 从菜品表根据id查询
        Dish dish = dishMapper.selectById(id);

        //2. 根据菜品的分类id从分类表中查询分类信息(名称)
        Category category = categoryMapper.selectById(dish.getCategoryId());
        dish.setCategoryName(category.getName());

        //3. 根据菜品id从口味表查询当前菜品口味列表
        LambdaQueryWrapper<DishFlavor> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(DishFlavor::getDishId, dish.getId());
        List<DishFlavor> dishFlavorList = dishFlavorMapper.selectList(wrapper);
        dish.setFlavors(dishFlavorList);

        return dish;
    }

    @Override
    public void update(Dish dish) {
        //1. 根据菜品id直接更新菜品基本信息
        dishMapper.updateById(dish);

        //2. 删除口味表中当前菜品的相关信息
        //delete from dish_flavor where dish_id = #{}
        LambdaQueryWrapper<DishFlavor> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(DishFlavor::getDishId,dish.getId());
        dishFlavorMapper.delete(wrapper);

        //3. 将修改之后口味列表重新保存到口味表中
        List<DishFlavor> dishFlavorList = dish.getFlavors();
        if (CollectionUtil.isNotEmpty(dishFlavorList)) {
            for (DishFlavor dishFlavor : dishFlavorList) {
                dishFlavor.setDishId(dish.getId());//设置菜品id
                dishFlavorMapper.insert(dishFlavor);
            }
        }
    }

    //批量禁用
    @Override
    public List<Dish> updateStatus(Integer status, List<Long> ids) {
        List<Dish> dishList = dishMapper.selectBatchIds(ids);
        for (Dish dish : dishList) {
            UpdateWrapper<Dish> wrapper = new UpdateWrapper<>();
            wrapper.eq("id", dish.getId());
            wrapper.set(status==0,"status", status);
            wrapper.set(status==1,"status", status);
            dishMapper.update(dish, wrapper);
        }
        return dishList;
    }

    //批量删除
    @Override
    public void deleteByIds(List<Long> ids) {
        List<Dish> dishList = dishMapper.selectBatchIds(ids);
        for (Dish dish : dishList) {
            if (dish.getStatus().equals(0)){
                throw new CustomException("该菜品是禁售的,不可删除!");
            }
            LambdaQueryWrapper<Dish> wrapper = new LambdaQueryWrapper<>();
            wrapper.eq(dish.getStatus().equals(1),Dish::getId,dish.getId());
            dishMapper.delete(wrapper);
            LambdaQueryWrapper<DishFlavor> wrapperFlavor = new LambdaQueryWrapper<>();
            wrapperFlavor.eq(DishFlavor::getDishId,dish.getId());
            dishFlavorMapper.delete(wrapperFlavor);
        }
    }

    @Override
    public List<Dish> findByCategoryId(Long categoryId) {
        LambdaQueryWrapper<Dish> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(Dish::getCategoryId,categoryId)
                .eq(Dish::getStatus,1);
        List<Dish> dishList = dishMapper.selectList(wrapper);

        //根据dishId在dish_flavor查找对应口味
        for (Dish dish : dishList) {
            LambdaQueryWrapper<DishFlavor> wrapper1 = new LambdaQueryWrapper<>();
            wrapper1.eq(DishFlavor::getDishId,dish.getId());
            List<DishFlavor> dishFlavorList = dishFlavorMapper.selectList(wrapper1);
            dish.setFlavors(dishFlavorList);
        }
       return dishList;
    }

    @Override
    public List<Dish> findByName(String name) {
        LambdaQueryWrapper<Dish> wrapper = new LambdaQueryWrapper<>();
        wrapper.like(Dish::getName,name);
        return dishMapper.selectList(wrapper);
    }
}
